package com.jiaquyun.jcps.ui.mine

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.TimeUtils
import com.jiaquyun.jcps.R
import com.jiaquyun.jcps.base.ViewModeActivity
import com.jiaquyun.jcps.entity.SignClock
import com.jiaquyun.jcps.mode.vm.MineViewModel
import com.jiaquyun.jcps.ui.LoadingDialog
import com.lemlin.network.HttpServiceStatus
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_mine_sign.*
import java.util.concurrent.TimeUnit

class MineSignActivity : ViewModeActivity<MineViewModel>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MineSignActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_sign

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, color = R.color.colorWhite, view = flSignBar)
    }

    override fun initView() {
        ivSignBack.setOnClickListener {
            finish()
        }
        tvSignDayTime.text =
            TimeUtils.millis2String(System.currentTimeMillis(), "yyyy-MM-dd")
        llSignSubmit.setOnClickListener {
            viewModel.signClockLiveData.value?.httpData?.data?.let {
                if (it.sb == 0) {
                    viewModel.goToWork()
                } else if (it.xb == 0) {
                    viewModel.goOffWork()
                }
            }
        }

        tvSignCurrentTime.text =
            TimeUtils.millis2String(System.currentTimeMillis(), "HH:mm:ss")
        val disposable = Observable.interval(0, 1000, TimeUnit.MILLISECONDS)
            .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                tvSignCurrentTime.text =
                    TimeUtils.millis2String(System.currentTimeMillis(), "HH:mm:ss")
            }
        viewModel.dispose().add(disposable)
    }

    override fun initData() {
        viewModel.punchTheClock()
    }

    override fun initViewMode() {
        viewModel.signClockLiveData.observe(this as LifecycleOwner, Observer { response ->
            when (response.statusCode) {
                HttpServiceStatus.Status.START -> {

                }
                HttpServiceStatus.Status.FAILURE -> {
                    LoadingDialog.dismiss()
                    toast(response.statusTip)
                }
                HttpServiceStatus.Status.SUCCESS -> {
                    LoadingDialog.dismiss()
                    response.httpData?.data?.let {
                        setFillData(it)
                    }
                }
            }
        })
        viewModel.goToWorkLiveData.observe(this as LifecycleOwner, Observer { response ->
            when (response.statusCode) {
                HttpServiceStatus.Status.START -> {
                    LoadingDialog.show(context = this)
                }
                HttpServiceStatus.Status.FAILURE -> {
                    LoadingDialog.dismiss()
                    toast(response.statusTip)
                }
                HttpServiceStatus.Status.SUCCESS -> {
                    viewModel.punchTheClock()
                }
            }
        })

        viewModel.goOffWorkLiveData.observe(this as LifecycleOwner, Observer { response ->
            when (response.statusCode) {
                HttpServiceStatus.Status.START -> {
                    LoadingDialog.show(context = this)
                }
                HttpServiceStatus.Status.FAILURE -> {
                    LoadingDialog.dismiss()
                    toast(response.statusTip)
                }
                HttpServiceStatus.Status.SUCCESS -> {
                    viewModel.punchTheClock()
                }
            }
        })
    }

    private fun setFillData(signClock: SignClock) {

        llSignSubmit.isEnabled = true

        tvSignSBTime.text = if (signClock.sb == 0 && signClock.sb_time.isNullOrEmpty())
            "未打卡" else signClock.sb_time + "已打卡"
        tvSignXBTime.text = if (signClock.xb == 0 && signClock.xb_time.isNullOrEmpty())
            "未打卡" else signClock.xb_time + "已打卡"
        ivSignSBSelect.visibility = if (signClock.sb == 0) View.GONE else View.VISIBLE
        ivSignXBSelect.visibility = if (signClock.xb == 0) View.GONE else View.VISIBLE

        if (signClock.sb == 0) {
            tvSignStatus.text = "上班打卡"
        } else if (signClock.xb == 0) {
            tvSignStatus.text = "下班打卡"
        } else {
            llSignSubmit.isEnabled = false
            tvSignStatus.text = "辛苦了"
            tvSignCurrentTime.visibility = View.GONE
        }
    }
}